package org.iks.store.business.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.iks.common.AjaxResult;
import org.iks.common.pojo.store.SecretKeyVo;
import org.iks.common.util.JwtUtil;
import org.iks.common.util.ServletUtil;
import org.iks.store.business.service.SessionEncipherService;
import org.iks.store.business.service.SessionService;
import org.iks.store.business.vo.secret.SecretCreateVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Author kzou
 * Date 2024/2/20
 * Description:
 */
@Tag(name = "会话密钥模块")
@RestController
@RequestMapping("secret")
public class SecretNegotiateController {

    @Autowired
    private SessionService sessionService;

    @Autowired
    private SessionEncipherService secretService;

    public AjaxResult<?> negotiateSecretKey(Integer sessionId, String key) {
        return AjaxResult.success();
    }

    @Operation(summary = "创建密钥")
    @PostMapping("create")
    public AjaxResult<?> createSecretKey(@RequestBody SecretCreateVo secretCreateVo) {
        String token = ServletUtil.getToken();
        String userAccount = JwtUtil.getUserAccount(token);
        int count = secretService.createSecretKey(userAccount, secretCreateVo);
        return AjaxResult.success(count);
    }

    @Operation(summary = "获取最新密钥")
    @PostMapping("query")
    public AjaxResult<SecretKeyVo> queryLatestSecretKey(Integer sessionId) {
        String token = ServletUtil.getToken();
        String userAccount = JwtUtil.getUserAccount(token);
        SecretKeyVo result = secretService.queryLatestSecretKey(userAccount,sessionId);
        return AjaxResult.success(result);
    }

    @Operation(summary = "密钥详情")
    @PostMapping("query/id")
    public AjaxResult<SecretKeyVo> querySecretKeyById(Integer secretKeyId) {
        SecretKeyVo result = secretService.queryById(secretKeyId);
        return AjaxResult.success(result);
    }

    @Operation(summary = "密钥列表")
    @PostMapping("query/list")
    public AjaxResult<List<SecretKeyVo>> queryListSecretKey(Integer sessionId) {
        List<SecretKeyVo> result = secretService.queryListSecretKey(sessionId);
        return AjaxResult.success(result);
    }

}
